/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.drakeet.multitype;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.app.Context;

import java.util.List;

/**
 * MultiTypeAdapter
 *
 * @param <T> Runtime Typecast
 */
public abstract class MultiTypeAdapter<T> extends BaseItemProvider {
    private List<T> typeList;

    /**
     * MultiTypeAdapter
     *
     * @param typeList List with runtime Type cast
     */
    protected MultiTypeAdapter(List<T> typeList) {
        this.typeList = typeList;
    }

    @Override
    public int getCount() {
        int listSize = 0;
        if (typeList != null) {
            listSize = typeList.size();
        }
        return listSize;
    }

    @Override
    public Object getItem(int position) {
        return typeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * getMyView
     *
     * @param position    Position
     * @param convertView Component
     * @param parent      Component Container
     * @param type        Runtime Type
     * @return Component
     * For T-type its Default to runtime
     */
    public abstract Component getMyView(int position, Component convertView, ComponentContainer parent, T type);

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        return getMyView(position, convertView, parent, typeList.get(position));
    }
}